package com.bsh.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.Exception.ApiException;
import com.bsh.service.ApiPaymentService;
import com.bsh.service.PaymentService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Constants;
import com.bsh.utils.Des3;

/**
 * 账单相关API
 * 
 * @author jane
 * @since 0.0.1
 * 
 */
@Controller
@RequestMapping(value = "/api/v1/payment", method = { RequestMethod.POST })
public class ApiPaymentController {
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	PaymentService paymentServ;
	@Autowired
	ApiPaymentService  apiPaymentService;

	@RequestMapping(value = "/list")
	@ResponseBody
	public ApiMessage paymentList(@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			ApiUtils.checkUserNameToken(dto);
			msg = new ApiMessage("200", "支付方式列表");
			List<Map<String, Object>> payment = paymentServ.getPaymentList(dto.getString("type"));
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("token", dto.getString("token"));
			data.put("paymentList", payment);
			msg.setData(Des3.encResponse(data));
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage(Constants.SYS_ERROR, Constants.MSG_FAILED);
		}
		return msg;
	}
	
	/**
	 * 获取充值方式
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/getPaymentList")
	@ResponseBody
	public ApiMessage getPaymentList(@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg=apiPaymentService.getPaymentList(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage(Constants.SYS_ERROR, Constants.MSG_FAILED);
			e.printStackTrace();
		}
		return msg;
	}
	

}
